﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using RegOnline.Views;
using RegOnline.RegOnlineService;

namespace RegOnline
{
    public partial class StudPage : Page
    {
        MainViewModel ac;
        private printForm PF;

        public StudPage()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ac = ((((Parent as Frame).Parent as Border).Parent as Grid).Parent as MainPage).Controller;
            if (ac == null) MessageBox.Show("ac нот лоадед!");
            ac.GroupLoaded += new EventHandler(ac_GroupLoaded);
            ac.SemetrLoaded += new EventHandler(ac_SemetrLoaded);
            ac.PrintInfoLoaded += new EventHandler(ac_PrintInfoLoaded);

            ac.LoadGroupStudent();


            grupNameComboBox.IsEnabled = false;


        }

        void ac_PrintInfoLoaded(object sender, EventArgs e)
        {
            PF.Specialnost = ac.PrintSpec;
            PF.Kafedra = ac.PrintKaf;
            PF.Facultet = ac.PrintFacultet;
            PF.FioPrepod = ac.PrintFio;
            PF.Show();
        }

        void ac_SemetrLoaded(object sender, EventArgs e)
        {
            semNumComboBox.ItemsSource = ac.Semesters;
            ac.CurrentSemestr = ac.Semesters.First();
            semNumComboBox.SelectedItem = ac.CurrentSemestr;
        }

        void ac_GroupLoaded(object sender, EventArgs e)
        {
            grupNameComboBox.ItemsSource = ac.Groups;
            grupNameComboBox.IsEnabled = true;
            grupNameComboBox.SelectedItem = ac.Groups.First();
            ac.CurrentGroup = grupNameComboBox.SelectedItem as RegOnlineService.Group;
        }

        private void grupNameComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (ac != null)
                {
                    ac.CurrentGroup = (grupNameComboBox.SelectedItem as RegOnlineService.Group);
                    ac.LoadSemestr((grupNameComboBox.SelectedItem as RegOnlineService.Group).IdGrup.Value);
                }
                else
                {
                    MessageBox.Show("Ac not loaded");
                }
            }
            catch (Exception)
            {
                
            }

        }
    

        private void semNumComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var semestr = semNumComboBox.SelectedItem as RegOnlineService.Semestr;
                ac.CurrentSemestr = semestr;
                upDataGrid.ItemsSource = semestr.Up;
            }
            catch(Exception)
            {

            }
        }

        private void upDataGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            try
            {
                var stud = ac.User;
                var grup = ac.CurrentGroup;
                var semestr = ac.CurrentSemestr;
                var Uch = upDataGrid.SelectedItem as RegOnlineService.UchPlan;
                ac.CurrentUchPlan = Uch;
                
                if (Uch != null)
                {
                    if (Uch.Dis == null || Uch.Vid == null)
                    {
                    }
                    else
                    {
                        NavigationService.Navigate(new Uri("/UchebnPlanPage", UriKind.Relative));
                    }
                }
            }
            catch (Exception)
            {
                
              
            }


        }

        private void upDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                var Uch = upDataGrid.SelectedItem as RegOnlineService.UchPlan;
                ac.CurrentUchPlan = Uch;

            }
            catch (Exception)
            {
                
            }


        }

        private void Del_Click(object sender, RoutedEventArgs e)
        {

        }

        private void printFrm_Click(object sender, RoutedEventArgs e)
        {
            string[] rim = new[]
                               {
                                   "I", "II", "III", "IV", "V", "VI"
                               };
            
            try
            {
                UchPlan w = upDataGrid.SelectedItem as UchPlan;

                PF = new printForm();

                //if (w.Vid.IdVid == 1 || w.Vid.IdVid == 3) //если контрольная или лабораторная
                //{
                //    PF.SetPrintOptions(printForm.PrintOptions.TitulOnly);
                //}
                //else
                //{
                //    PF.SetPrintOptions(printForm.PrintOptions.All);
                //}
                
                ac.GetPrintInfo(w.IdUP.Value,w.IdUP.Value);

                PF.Tema = w.Ws.theme;
                PF.Kafedra = ""; 
                PF.Kod = ac.User.GetAsStudent().zach.nZach;
                PF.Fio = ac.User.Fio;
                PF.NomerZach = ac.User.GetAsStudent().zach.nZach;
                PF.Disciplina = w.Dis.DisName;
                PF.Gruppa = ac.CurrentGroup.grupName;
                PF.ZaglavieTitula =w.Vid.VidWorkName;
                PF.FioPrepod = "";
                PF.Facultet = "";
                PF.Specialnost = "";
                int k = ((semNumComboBox.SelectedItem as Semestr).SemNum.Value+1)/2;
                PF.Kyrs = rim[k];
                PF.RegDate = w.Ws.RegDate.HasValue ? w.Ws.RegDate.Value.ToShortDateString() : "";
                PF.RegNumber = w.Ws.RegNum.HasValue ? w.Ws.RegNum.Value.ToString() : "";
                
            }
            catch (Exception ex)
            {
                
                ErrorWindow err = new ErrorWindow(ex);
                err.Show();
            }
            
        }

        

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Views.printForm PF = new printForm();
            PF.SetPrintOptions(printForm.PrintOptions.TitulOnly);

            PF.Tema = "Влияние психосоматических колебаний мирового разума на состояние геополитической обстановке в напряжонных военно-конфликтных раойнах депрессивно-развитых стран";
            PF.Kafedra = "Информационные системы и технологии";
            PF.Fio = ac.User.Fio;
            PF.Disciplina = "Психопрограммирование";
            PF.Gruppa = "ПП-01-12";
            PF.ZaglavieTitula = "Задание на курсовой проект(работу)";
            PF.FioPrepod = "Мечеикринский П. М.";
            PF.RegDate = "01.04.2013";
            PF.RegNumber = "230edas";
            PF.Show();
        }

    }
}
